package org.dromara.customer.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.customer.domain.bo.TaStoreShoppingCartBo;
import org.dromara.customer.domain.vo.TaStoreShoppingCartVo;
import org.dromara.merchants.api.RemoteGoodsService;
import org.springframework.stereotype.Service;
import org.dromara.customer.domain.bo.TaShoppingCartBo;
import org.dromara.customer.domain.vo.TaShoppingCartVo;
import org.dromara.customer.domain.TaShoppingCart;
import org.dromara.customer.mapper.TaShoppingCartMapper;
import org.dromara.customer.service.ITaShoppingCartService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 购物车Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-09
 */
@RequiredArgsConstructor
@Service
public class TaShoppingCartServiceImpl implements ITaShoppingCartService {

    private final TaShoppingCartMapper baseMapper;

    @DubboReference
    private final RemoteGoodsService remoteGoodsService;

    /**
     * 查询购物车
     */
    @Override
    public TaShoppingCartVo queryById(Long shoppingCartId) {
        return baseMapper.selectVoById(shoppingCartId);
    }

    /**
     * 查询购物车列表
     */
    @Override
    public TableDataInfo<TaStoreShoppingCartVo> queryPageList(TaShoppingCartBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        LambdaQueryWrapper<TaShoppingCart> lqw = buildQueryWrapper(bo);
        Page<TaStoreShoppingCartVo> result = baseMapper.selectGroupPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询购物车列表
     */
    @Override
    public List<TaShoppingCartVo> queryList(TaShoppingCartBo bo) {
        LambdaQueryWrapper<TaShoppingCart> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }


    private LambdaQueryWrapper<TaShoppingCart> buildQueryWrapper(TaShoppingCartBo bo) {
        LambdaQueryWrapper<TaShoppingCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TaShoppingCart::getUserId, bo.getUserId());
        lqw.eq(bo.getGoodsId() != null, TaShoppingCart::getGoodsId, bo.getGoodsId());
        return lqw;
    }

    /**
     * 新增购物车
     *
     * @param bo
     */
    @Override
    public List<TaStoreShoppingCartBo> insertByBo(List<TaStoreShoppingCartBo> bo) {
        bo.forEach(item -> {
            item.getGoodsList().forEach(goods -> {
                goods.setStoreId(item.getStoreId());
                goods.setStoreName(item.getStoreName());
                goods.setUserId(LoginHelper.getUserId());
                String price = remoteGoodsService.queryGoodsById(goods.getGoodsId()).getPrice();
                goods.setAmount(String.valueOf(new BigDecimal(price).multiply(new BigDecimal(goods.getNumber()))));
                goods.setCreateTime(new Date());
                TaShoppingCart add = MapstructUtils.convert(goods, TaShoppingCart.class);
                validEntityBeforeSave(add);
                baseMapper.insertOrUpdate(add);
                goods.setId(add.getId());
            });
        });

        return bo;
    }


    /**
     * 修改购物车
     */
    @Override
    public Boolean updateByBo(TaShoppingCartBo bo) {
        TaShoppingCart update = MapstructUtils.convert(bo, TaShoppingCart.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaShoppingCart entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除购物车
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
